from .models import User
from django.contrib.auth.backends import ModelBackend
from django.db.models import Q

def jwt_response_payload_handler(token, user=None, request=None):
    """
    这时user对象已经有了 , 想拿什么拿什么
    """
    return {
        'token': token,
        'id': user.id,
        'username': user.username
    }


def get_user(username):
    return User.objects.filter(Q(username=username)|Q(mobile=username)).first()



class UsernameMobileAuthBackend(ModelBackend):
    def authenticate(self, request, username=None, password=None, **kwargs):
        user = get_user(username)
        if user and user.check_password(password):
            return user










